package cn.myplus.uc.user

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.hateoas.CollectionModel
import org.springframework.hateoas.EntityModel
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController


/**
 * @project myplus5
 * @description 用户接口
 * @author libo
 * @date 2023-10-03 15:24:00
 */
@RestController
class UserController(@Autowired val userService: UserService) {

    private val logger: Logger = LoggerFactory.getLogger(UserController::class.java)

    @Autowired
    private lateinit var userModelAssembler: UserModelAssembler

    /**
     * @return 用户列表.
     */
    @GetMapping("/users")
    fun getUsers(userModel: UserModel?): ResponseEntity<CollectionModel<UserModel>>? {
        logger.debug("查询用户：参数${userModel.toString()}")
        val returnList = mutableListOf<UserModel>()
        val users: MutableIterable<User> = userService.getUsers()
        val toCollectionModel = userModelAssembler.toCollectionModel(users)
//        users.forEach {
//            returnList.add(userModelAssembler.toModel(it))
//        }
        return ResponseEntity<CollectionModel<UserModel>>(toCollectionModel, HttpStatus.OK)
    }

    /**
     * @return 用户信息.
     * @param id 用户id
     * @author libo
     */
    @GetMapping("/user/{id}")
    fun getUser(@PathVariable("id") id: String): ResponseEntity<UserModel> {
        val user = userService.getUserById(id)
        return ResponseEntity<UserModel>(userModelAssembler.toModel(user), HttpStatus.OK)
    }
}